upload images local send to telegram bot API
“uploadController.php”
//ค่าที่ถูกส่งมาจากหน้าบ้าน
$imgbase64 = $req->imgbase64;
//Set ที่อยู่ของรูปภาพ “Path”
$path = 'tws_Images/slip/' . $order_id . '/';
//เช็ค path ถ้า path ไม่ว่างให้ลบโฟลเดอร์
if ($order_payment->payment_slip != null) {
Storage::deleteDirectory($path);
}
//ส่งรูปภาพไป decode และ บนทึก path to DB
if ($req->imgbase64) {
$fullPath = $this->uploadBase64($imgbase64, $path);
$order_payment->payment_slip = $fullPath;
$order_payment->img_at = Carbon::now();
$order_payment->save();
} else {
dd('Base64 not match');
}
//send to fn telegram on controller $this->telegramNotiflyWithImage($fullPath);
“Controller.php”
//base64_decode
public function uploadBase64($base64_image, $path)
{
try {
if (preg_match('/^data:image\/(\w+);base64,/', $base64_image)) {
$data = substr($base64_image, strpos($base64_image, ',') + 1);
$base64_decode = base64_decode($data);
$extension = explode('/', explode(':', substr($base64_image, 0, strpos($base64_image, ';')))[1])[1];
$fileName = strtotime(Carbon::now()) . rand(1, 100) . '.' . $extension;
$now = strtotime(Carbon::now());
$fullPath = $path . $fileName;
$disk = Storage::disk('local');
$disk->put($fullPath, $base64_decode);
return $fullPath;
} else {
dd('Base64 not match');
}
} catch (\Exception $e) {
$sMessageGroup['text'] = "** Error **" .
"\nError: " . $e->getMessage();
$this->telegramNotifyGroup($sMessageGroup);
}
}
//telegramNotifyImg_Controller_images
public function telegramNotiflyWithImage($fullPath)
{
try {
$BOT_TOKEN=env('TELEGRAM_TOKEN');
$chat_id=env('ID_GROUP');
define('BOTAPI','https://api.telegram.org/bot' . $BOT_TOKEN .'/');
$real_path = Storage::disk('local')->path($fullPath);
// dd($fullPath);
$cfile = new \CURLFile($real_path , 'image/jpg', 'slips');
$data = [
'chat_id' => $chat_id ,
'photo' => $cfile
];
$ch = curl_init(BOTAPI.'sendPhoto');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
} catch (\Exception $e) {
$sMessageGroup['text'] = "** Error **" .
"\nError: " . $e->getMessage();
$this->telegramNotifyGroup($sMessageGroup);
}
}
Cr.เฮียบอย โมบายเซอร์วิส